package com.oba.king.sysmanager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.tree.DefaultTreeModel;

import com.oba.base.service.BaseBean;
import com.oba.base.util.TreeUtil;

public class DeptBean extends BaseBean<Dept, DeptService> {

	private DefaultTreeModel deptTree;
	private Map<Long, Dept> deptMap = new HashMap<Long, Dept>();
	private Dept curDept;
	private Dept dept;

	private boolean showAddDept;

	public void doAddDept() {
		dept = new Dept();
		dept.setNewBo(true);
		dept.setParent(curDept);
		showAddDept = true;
	}

	public void doUpdateDept() {
		dept = curDept;
		showAddDept = true;
	}

	public void doCloseDept() {
		showAddDept = false;
	}

	public void doSaveDept() {
		if (dept.isNewBo()) {
			String subSerial = getService().getDao().getNextSerial(Dept.class, "serial", curDept.getSerial());
			dept.setSerial(subSerial);
			TreeUtil.addChrid(curDept.getNode(), dept);
			deptTree.reload();
		}
		getService().save(dept);
		deptMap.put(dept.getId(), dept);
		showAddDept = false;
		curDept = dept;
	}

	public void doDeleteDept() {
		deptTree.removeNodeFromParent(curDept.getNode());
		getService().del(curDept);
		curDept = curDept.getParent();
	}

	public void selectDept() {
		String idStr = getParam("deptId");
		curDept = deptMap.get(Long.parseLong(idStr));
	}

	@Override
	public String doFind() {
		return null;
	}

	@Override
	public void init() {
		showAddDept = false;
		List<Dept> list = getService().getDao().findAll();
		curDept = list.get(0);
		for (Dept d : list) {
			deptMap.put(d.getId(), d);
		}
		deptTree = TreeUtil.createTree(list);
	}

	public DefaultTreeModel getDeptTree() {
		return deptTree;
	}

	public void setDeptTree(DefaultTreeModel deptTree) {
		this.deptTree = deptTree;
	}

	public Dept getCurDept() {
		return curDept;
	}

	public void setCurDept(Dept curDept) {
		this.curDept = curDept;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public boolean isShowAddDept() {
		return showAddDept;
	}

	public void setShowAddDept(boolean showAddDept) {
		this.showAddDept = showAddDept;
	}

}
